package ru.osu.acm.common.industrial;

import ru.osu.acm.common.entities.Team;
import ru.osu.acm.common.entities.Problem;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * User: aleksey
 * Date: Sep 22, 2009
 * Time: 12:30:52 PM
 */
public class TeamFactory extends Factory {
    public static Team getTeam(int teamId, Connection con, boolean closeConnection) {
        Team team = null;

        try {
            PreparedStatement statement = con.prepareStatement("select * from team where id = ?");
            statement.setInt(1, teamId);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                team = new Team(
                        resultSet.getInt("id"),
                        resultSet.getString("login").trim(),
                        resultSet.getString("pass").trim(),
                        resultSet.getString("teamname").trim());
            }
            resultSet.close();
            statement.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        closeConnection(con, closeConnection);
        return team;
    }

    public static ArrayList<Team> getAllTeams(Connection con, boolean closeConnection) {
        ArrayList<Team> teams = new ArrayList<Team>();

        try {
            PreparedStatement statement = con.prepareStatement("select * from team order by id");
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                teams.add(new Team(
                        resultSet.getInt("id"),
                        resultSet.getString("login").trim(),
                        resultSet.getString("pass").trim(),
                        resultSet.getString("teamname").trim()));
            }
            resultSet.close();
            statement.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        closeConnection(con, closeConnection);
        return teams;
    }

    public static ArrayList<Team> getContestTeams(int contestId, Connection con, boolean closeConnection) {
        ArrayList<Team> teams = new ArrayList<Team>();

        try {
            PreparedStatement statement = con.prepareStatement("select * from contestteam where contest = ? order by id");
            statement.setInt(1, contestId);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Team team = getTeam(resultSet.getInt("team"), con, false);
                teams.add(team);
            }
            resultSet.close();
            statement.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        closeConnection(con, closeConnection);
        return teams;
    }

    public static void removeTeamFromContest(int teamId, int contestId, Connection con, boolean closeConnection) {
        try {
            PreparedStatement statement = con.prepareStatement("delete from contestteam where team = ? and contest = ?");
            statement.setInt(1, teamId);
            statement.setInt(2, contestId);
            statement.executeUpdate();
            statement.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        closeConnection(con, closeConnection);
    }

    public static void addTeamToContest(int teamId, int contestId, Connection con, boolean closeConnection) {
        boolean alreadyAdded = false;

        try {
            PreparedStatement statement = con.prepareStatement("select * from contestteam where contest = ? and team = ?");
            statement.setInt(1, contestId);
            statement.setInt(2, teamId);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                alreadyAdded = true;
            }
            resultSet.close();
            statement.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        if (alreadyAdded) {
            closeConnection(con, closeConnection);
            return;
        }

        try {
            int newId = getNextId("contestteam", con, false);
            PreparedStatement statement = con.prepareStatement("insert into contestteam(id, team, contest) values (?, ?, ?)");
            statement.setInt(1, newId);
            statement.setInt(2, teamId);
            statement.setInt(3, contestId);
            statement.executeUpdate();
            statement.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        closeConnection(con, closeConnection);
    }
}
